﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DwoDimentionArray
{
    class Program
    {
        static void Main(string[] args)
        {
            string ch;
            do
            {
                Console.Clear();
                Console.WriteLine("Use buttons 1-7 to choose the task.");
                Console.WriteLine("1 - Replacing the fifth element in each raw with sum of four previous elements");
                Console.WriteLine("2 - Calculating SUM of the Main diagonal of the sqare matrix");
                Console.WriteLine("3 - Even and odd elements of Matrix");
                Console.WriteLine("4 - Maximum of minimum elements of each column");
                Console.WriteLine("5 - Summary of each column and each raw");
                Console.WriteLine("6 - Max summary of raw and number of this raw");
                Console.WriteLine("7 - Product of not Null elements");
                Console.WriteLine("Choose the task:");
                int choose;
                choose = Int32.Parse(Console.ReadLine());

                switch (choose)
                {
                    case 1:
                        ReplacingFifthElemetInEachRawWithSumOfPrevious();
                        break;
                    case 2:
                        SummarizeMainDiagonal();
                        break;
                    case 3:
                        CountEvenAndOddElements();
                        break;
                    case 4:
                        MaxOfMin();
                        break;
                    case 5:
                        SummaryColumnsAndRaws();
                        break;
                    case 6:
                        MaxSumOfRaw();
                        break;
                    case 7:
                        ProductOfNotNullElements();
                        break;
                    default:
                        Console.WriteLine("Error!");
                        break;
                }
                Console.WriteLine("Do you want to continue? y/n");
                ch = Console.ReadLine().ToLower();
            } while (ch == "y");
        }

        static void ProductOfNotNullElements()
        {
            int product = 1;
            Console.WriteLine("Create a two-dimentional array.");
            Console.Write("Enter amount of Raws (n): ");
            int countRaw = Int32.Parse(Console.ReadLine());
            Console.Write("Enter amount of Columns (m): ");
            int countColumn = Int32.Parse(Console.ReadLine());
            int[,] newArray = FillDwoDimArray(countRaw, countColumn, -10, 11);
            Console.Clear();
            Console.WriteLine("Array: ");
            PrintArrayToConsole(newArray);
            for (int i = 0; i < countRaw; i++)
            {
                for (int j = 0; j < countColumn; j++)
                {
                    if (newArray[i,j]!=0)
                   product = product * newArray[i, j];
                }
            }
            Console.WriteLine("Final product: {0}", product);
        }

        static void MaxSumOfRaw()
        {

            
            int sumOfRaw = 0;
            Console.WriteLine("Create a two-dimentional array.");
            Console.Write("Enter amount of Raws (n): ");
            int countRaw = Int32.Parse(Console.ReadLine());
            Console.Write("Enter amount of Columns (m): ");
            int countColumn = Int32.Parse(Console.ReadLine());
            int[,] newArray = FillDwoDimArray(countRaw, countColumn, 10, 101);
            Console.Clear();
            Console.WriteLine("Array: ");
            PrintArrayToConsole(newArray);
            int[] sumsOfRaws = new int[countRaw];
            for (int i = 0; i < countRaw; i++)
            {
                sumOfRaw = 0;
                for (int j = 0; j < countColumn; j++)
                  
                {
                    sumOfRaw = sumOfRaw + newArray[i, j];
                }
                sumsOfRaws[i] = sumOfRaw;
                
                Console.WriteLine("Sum of Raw [{0}]: {1}", i, sumOfRaw);
            }
            Console.WriteLine("Max sum in the raw: {0}", sumsOfRaws.Max());
            Console.WriteLine("Index of raw with max sum of elements: [{0}]", Array.IndexOf(sumsOfRaws, sumsOfRaws.Max()));



            }

        static void SummaryColumnsAndRaws()
        {
            int b = 0;
            int a = 0;
            int sumOfColumn = 0;
            int sumOfRaw = 0;
            Console.WriteLine("Create a two-dimentional array.");
            Console.Write("Enter amount of Raws (n): ");
            int countRaw = Int32.Parse(Console.ReadLine());
            Console.Write("Enter amount of Columns (m): ");
            int countColumn = Int32.Parse(Console.ReadLine());
            int[,] newArray = FillDwoDimArray(countRaw, countColumn, -100, 101);
            Console.Clear();
            Console.WriteLine("Array: ");
            PrintArrayToConsole(newArray);

            for (int i=0; i<countRaw; i++)
            {
                for (int j=0; j<countColumn; j++)
                {
                    sumOfRaw = sumOfRaw + newArray[i, j];
                }

                a++;
                Console.WriteLine("Sum of Raw {0}: {1}", a, sumOfRaw);
            }
            Console.WriteLine();
            for (int i = 0; i < countColumn; i++)
            {
                for (int j = 0; j < countRaw; j++)
                {
                    sumOfColumn = sumOfColumn + newArray[j, i];
                }
               b++;
                Console.WriteLine("Sum of Raw {0}: {1}", b, sumOfColumn);
            }
        }

        static void MaxOfMin()
        {

            
            Console.WriteLine("Create a two-dimentional array.");
            Console.Write("Enter amount of Raws (n): ");
            int countRaw = Int32.Parse(Console.ReadLine());
            Console.Write("Enter amount of Columns (m): ");
            int countColumn = Int32.Parse(Console.ReadLine());
            int[,] newArray = FillDwoDimArray(countRaw, countColumn, -100, 100);
            Console.WriteLine("Array: ");
            PrintArrayToConsole(newArray);
            int[] minElements = new int[countColumn];
            for (int i = 0; i < countColumn; i++)
            {
                int minValOfColumn = Int32.MaxValue;
                for (int j = 0; j < countRaw; j++)
                {
                    if (newArray[j, i] < minValOfColumn)
                        minValOfColumn = newArray[j, i];
                }
                minElements[i] = minValOfColumn;
            }
            Console.WriteLine();
            Console.Write("Minimum elements from each column: ");
            foreach (var item in minElements)
            {
                Console.Write("{0}, ", item);
            }
            Console.WriteLine();
            minElements.Max();
            Console.WriteLine("Max element of all minimm of columns: {0}", minElements.Max());
            Console.WriteLine();

        }

        static void CountEvenAndOddElements()
        {
            Console.Clear();
            int amountOfEvenElements = 0;
            int amountOfOddElements = 0;
            Console.WriteLine("Create a two-dimentional array.");
            Console.Write("Enter amount of Raws: ");
            int countRaw = Int32.Parse(Console.ReadLine());
            Console.Write("Enter amount of Columns: ");
            int countColumn = Int32.Parse(Console.ReadLine());
            int[,] newArray = FillDwoDimArray(countRaw, countColumn, 1, 21);
            PrintArrayToConsole(newArray);

            for (int i = 0; i < countRaw; i++)
            {
                for (int j = 0; j < countColumn; j++)
                {
                    if ((newArray[i, j] % 2) == 0)
                        amountOfEvenElements++;
                    else if ((newArray[i, j] % 2) != 0)
                        amountOfOddElements++;
                }
            }
            Console.WriteLine("Amount of Even elements in the Matrix: {0}", amountOfEvenElements);
            Console.WriteLine("Amount of Odd elements in the Matrix: {0}", amountOfOddElements);
        }

        static void SummarizeMainDiagonal()
        {
            int sumOfMainDiagonal = 0;
            Console.WriteLine("Enter size of the square array: ");
            int arraySize = Int32.Parse(Console.ReadLine());
            int[,] squareMatrix = FillDwoDimArray(arraySize, arraySize, -100, 101);
            Console.Clear();
            Console.WriteLine("Array: ");
            Console.WriteLine();
            PrintArrayToConsole(squareMatrix);
            for (int i = 0; i < arraySize; i++)
            {
                sumOfMainDiagonal = sumOfMainDiagonal + squareMatrix[i, i];
            }
            Console.WriteLine();
            Console.WriteLine("Summary of the Main diagonal is: {0}", sumOfMainDiagonal);

            int sumOfLateralDiagonal = 0;
            for (int i = (arraySize - 1); i >= 0; i--)
            {
                for (int j = 0; j < arraySize; j++)
                {
                    if ((i + j) == (arraySize - 1))
                        sumOfLateralDiagonal = sumOfLateralDiagonal + squareMatrix[i, j];
                    
                }
            }
            Console.WriteLine();
            Console.WriteLine("Summary of the Lateral diagonal is: {0}", sumOfLateralDiagonal);
        }
        

        static void ReplacingFifthElemetInEachRawWithSumOfPrevious()
        {

            int sumOfElementsInRaw = 0;

            Console.Clear();
            int[,] newTestArray = FillDwoDimArray(4, 5, -10, 11);
            Console.WriteLine("First Array:");
            Console.WriteLine();
            PrintArrayToConsole(newTestArray);

            Console.WriteLine("Press 'Enter' to continue...");
            Console.ReadKey();

            for (int i = 0; i < 4; i++)
            {
                sumOfElementsInRaw = 0;
                for (int j = 0; j < 5; j++)
                {
                    if (j < 4)
                    {
                        sumOfElementsInRaw = (sumOfElementsInRaw + newTestArray[i, j]);
                    }
                    newTestArray[i, 4] = sumOfElementsInRaw;
                }
            }
            Console.Clear();
            Console.WriteLine("Array after calculation: ");
            Console.WriteLine();
            PrintArrayToConsole(newTestArray);

            Console.ReadKey();
        }

        static int[,] FillDwoDimArray(int amountOfRaws, int amountOfColumns, int minRnd, int maxRnd)
        {
            int[,] array = new int[amountOfRaws, amountOfColumns];
            Random rnd = new Random();
            for (int i = 0; i < amountOfRaws; i++)
            {
                for (int j = 0; j < amountOfColumns; j++)
                {
                    array[i, j] = rnd.Next(minRnd, maxRnd+1);
                }
            }
            return array;
        }

        static void PrintArrayToConsole(int[,] arrayForPrint)
        {
            for (int i = 0; i < arrayForPrint.GetLength(0); i++)
            {
                for (int j = 0; j < arrayForPrint.GetLength(1); j++)
                {
                    Console.Write("{0}\t", arrayForPrint[i, j]);
                }
                Console.WriteLine();
            }
        }

    }
}
